package edu.controller;


import cn.hutool.crypto.digest.MD5;
import edu.domain.Orders;
import edu.domain.User;
import edu.service.GoodsService;
import edu.service.OrdersService;
import edu.service.UserService;
import edu.support.base.controller.AdminBaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * (GoodsCollect)表控制层
 *
 * @date 11:30:11
 */
@Controller
@RequestMapping("user")
public class UserController extends AdminBaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrdersService ordersService;

    /**
     * 更新个人信息
     *
     * @param user
     * @return
     */
    @RequestMapping("update")
    public String update(User user, RedirectAttributes attributes, MultipartFile file) {
        if (StringUtils.isNotBlank(user.getPassword())) {
            user.setPassword(MD5.create().digestHex(user.getPassword()));
        } else {
            user.setPassword(null);
        }

        user.setId(loginUserId());
        userService.saveOrUpdate(user);
        return refresh("更新成功", attributes);
    }


    /**
     * 个人信息
     *
     * @return
     */
    @RequestMapping("info")
    public String info(Model model) {
        User user = userService.getById(loginUserId());
        model.addAttribute("user", user);
        return "user/info";
    }


}